package translinkApp.Activities;

import java.util.ArrayList;

import translinkApp.Adaptors.JourneyAdapter;
import translinkApp.Data.Journey.Search.JourneySearch;
import translinkApp.Data.Journey.Search.JourneySearchList;
import translinkApp.Utilities.DataResource;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class FavouritesMenuActivity extends Activity {

	private ListView listView;
	private final int MENU_SELECT_ALL = 1;
	private final int MENU_DESELECT_ALL = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favouritesmenu);

		listView = (ListView) findViewById(R.id.modifyLocationsListView);

		Button backButton = (Button) findViewById(R.id.modifyLocationBack);
		backButton.setOnClickListener(backButtonListener);

		Button deleteButton = (Button) findViewById(R.id.modifyLocationDelete);
		deleteButton.setOnClickListener(deleteButtonListener);

		UpdateAdaptor();

	}

	OnClickListener backButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}

	};

	OnClickListener deleteButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			JourneyAdapter adaptor = ((JourneyAdapter) listView.getAdapter());
			ArrayList<CheckBox> boxes = adaptor.getCheckBoxes();

			for (int i = 0; i < boxes.size(); i++) {
				CheckBox box = boxes.get(i);
				JourneySearch search = (JourneySearch) box.getTag();

				if (box.isChecked()) {

					DataResource.journeysList.getData().remove(search);
				}
			}

			Toast.makeText(FavouritesMenuActivity.this, "Selected journeys deleted.",
					Toast.LENGTH_SHORT).show();

			UpdateAdaptor();
		}
	};

	private void UpdateAdaptor() {

		DataResource.journeysList.save(this);
		JourneySearchList journeyData = DataResource.journeysList.getData();

		JourneyAdapter journeyAdaptor = new JourneyAdapter(this, journeyData);

		listView.setAdapter(journeyAdaptor);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem selectAllItem = menu.add(0, MENU_SELECT_ALL, 0, "Select All");
		selectAllItem.setIcon(android.R.drawable.ic_menu_agenda);

		MenuItem deselectAllItem = menu.add(0, MENU_DESELECT_ALL, 0, "Deselect All");
		deselectAllItem.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case MENU_SELECT_ALL:
			setCheckboxesState(true);
			return true;

		case MENU_DESELECT_ALL:
			setCheckboxesState(false);
			return true;

		}
		return false;
	}

	private void setCheckboxesState(boolean checked) {
		JourneyAdapter adaptor = ((JourneyAdapter) listView.getAdapter());
		for (CheckBox box : adaptor.getCheckBoxes()) {
			box.setChecked(checked);
		}
	}

}
